import RestClient from "@/driver/RestClient"
import { LocalStore } from "@/driver/LocalStore"

const WeirdService = {
    _KEY_DRAFT: 'weird.draft',
    /**
     * get weird list.
     * @param key
     * @param page
     * @param range
     * @param callback
     * @param failCallback
     */
    getWeirdList(key, page, range, callback, failCallback) {
        RestClient.get('/api/v1/weirds?key={0}&page={1}&range={2}'.formats(key, page, range), callback, failCallback)
    },

    /**
     * save weird.
     * @param data
     * @param callback
     * @param failCallback
     */
    saveWeird(data, callback, failCallback) {
        RestClient.post('/api/v1/weirds', data, callback, failCallback)
    },
    /**
     * modify weird.
     * @param data
     * @param callback
     * @param failCallback
     */
    modifyWeird(data, callback, failCallback) {
        RestClient.put(`/api/v1/weirds/${data.id}`, data, callback, failCallback)
    },
    /**
     * del weird.
     * @param id
     * @param callback
     * @param failCallback
     */
    delWeird(id, callback, failCallback) {
        RestClient.delete(`/api/v1/weirds/${id}`, callback, failCallback)
    },
    /**
     * 点赞.
     * @param id
     * @param callback
     * @param failCallback
     */
    addFavour(id, callback, failCallback) {
        RestClient.post(`/api/v1/weirds/${id}/actions/favour`, {}, callback, failCallback)
    },
    /**
     * 评论.
     * @param id
     * @param comment
     * @param callback
     * @param failCallback
     */
    addComment(id, comment, callback, failCallback) {
        RestClient.post(`/api/v1/weirds/${id}/comments`, {comment: comment}, callback, failCallback)
    },
    /**
     * 获取评论列表.
     * @param id
     * @param page
     * @param callback
     * @param failCallback
     */
    getCommentList(id, page, callback, failCallback) {
        RestClient.get(`/api/v1/weirds/${id}/comments?page=${page}`, callback, failCallback)
    },

    /**
     * 保存草稿.
     * @param content
     */
    saveDraft(content) {
        LocalStore.save(this._KEY_DRAFT, content)
    },
    /**
     * 获取草稿.
     */
    getDraft(call) {
        return LocalStore.get(this._KEY_DRAFT, call)
    },
    /**
     * 删除草稿.
     */
    delDraft() {
        LocalStore.remove(this._KEY_DRAFT)
    },
}

export default WeirdService